package org.parademo.equations.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.parademo.equations.base.Proposal;
import org.parademo.equations.base.Vote;

/**i
 *
 * @author Ajit Skanda Kumaraswamy
 */
public class DBCalculations {

    /**
     * The username and password for accessing the DB.
     */
    private String username = "root";
    private String password = "root123";
    
    /**
     * The DB.
     */
    String dbName = "parademian";
    
    /**
     * The address of server hosting the DB.
     */
    private String server = "localhost";

    /**
     * The url to connect to the DB.
     */
    private String url = null;

    /**
     * The connection object used to interract with the DB.
     */
    private Connection connection = null;

    /**
     * The Statement object used to interract with the DB.
     */
    private Statement stmt = null;

    /**
     * The ResultSet object used to interract with the DB.
     */
    private ResultSet rs = null;

    /**
     * Default constructor.
     */
    public DBCalculations() {}

    /**
     * Detailed constructor.
     */
    public DBCalculations(String username, String password, String dbName, String server) {

        this.setUsername(username);
        this.setPassword(password);
        this.setDbName(dbName);
        this.setServer(server);
    }
 

    /**
     * The method used to connect to the DB.
     */
    public void accessDB(long contentID) {
        try {

            //will this work across the entire class at all times??
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            this.setUrl("jdbc:mysql://" + getServer() + "/" + getDbName());
            this.setConnection(DriverManager.getConnection(getUrl(), getUsername(), getPassword()));

            setStmt(connection.createStatement());
            setRs(stmt.executeQuery("Select * from `node`"));

            Proposal proposal = new Proposal();
            Vector<Vote> votes = new Vector();
            String temp = "";
            int temp1 = 0;
            float temp2 = 0;

            while(rs.next()) {

               //time-difference comparision?
               
               
               /*
               System.out.println("vote_id : " + getRs().getInt("vote_id") + "\n");
               System.out.println("content_type : " + getRs().getString(2) + "\n");
               System.out.println("content_id : " + getRs().getInt("content_id") + "\n");
               System.out.println("value : " + getRs().getFloat("value") + "\n\n");
                */
           }

        } catch (SQLException sqle) {

            Logger.getLogger(DBCalculations.class.getName()).log(Level.SEVERE, null, sqle);

        } catch (ClassNotFoundException cnf) {

            Logger.getLogger(DBCalculations.class.getName()).log(Level.SEVERE, null, cnf);

        } catch (Exception e) {

            System.out.println("Error !!");
            Logger.getLogger(DBCalculations.class.getName()).log(Level.SEVERE, null, e);
        }
        finally {


            if (getRs() != null) {
                try {
                    getRs().close();
                } catch (SQLException sqlEx) {} // ignore

                setRs(null);
            }

            if (getStmt() != null) {
                try {
                    getStmt().close();
                } catch (SQLException sqlEx) {} // ignore

                setStmt(null);
            }

            if (getConnection() != null) {
                try {
                    getConnection().close();
                } catch (SQLException sqlEx) {} // ignore

                setConnection(null);
            }
        }
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param userName the username to set
     */
    public void setUsername(String userName) {
        this.username = userName;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the dbName
     */
    public String getDbName() {
        return dbName;
    }

    /**
     * @param dbName the dbName to set
     */
    public void setDbName(String dbName) {
        this.dbName = dbName;
    }

    /**
     * @return the server
     */
    public String getServer() {
        return server;
    }

    /**
     * @param server the server to set
     */
    public void setServer(String server) {
        this.server = server;
    }

    /**
     * @return the url
     */
    public String getUrl() {
        return url;
    }

    /**
     * @param url the url to set
     */
    public void setUrl(String url) {
        this.url = url;
    }

    /**
     * @return the connection
     */
    public Connection getConnection() {
        return connection;
    }

    /**
     * @param connection the connection to set
     */
    public void setConnection(Connection connection) {
        this.connection = connection;
    }

    /**
     * @return the stmt
     */
    public Statement getStmt() {
        return stmt;
    }

    /**
     * @param stmt the stmt to set
     */
    public void setStmt(Statement stmt) {
        this.stmt = stmt;
    }

    /**
     * @return the rs
     */
    public ResultSet getRs() {
        return rs;
    }

    /**
     * @param rs the rs to set
     */
    public void setRs(ResultSet rs) {
        this.rs = rs;
    }

    public static void main(String[] argv) {

        DBCalculations test = new DBCalculations("root", "root123", "parademian", "localhost");
        test.accessDB(35246354);

    }
}
